package com.china.school.Pointcut的12种用法;

import com.china.school.pojo.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;


@Aspect
@ComponentScan({"com.china.school"}) //<context:component-scan base-package="xx"/>
@EnableAspectJAutoProxy
public class WithTest {


    /**
      within(类型表达式) 目标对象target的类型是否和within指定的类型匹配
     其实就是包名匹配法
     */
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(WithTest.class);
        User user = ctx.getBean(User.class);
        user.eat();

    }

    /**
     匹配到包级别
     */
    @Pointcut("within(com.china.school.pojo.*)")
    public void pointcut(){}

     @Before("WithTest.pointcut()")
    public void before(){
        System.out.println("------before---------");
    }

    @After("pointcut()")
    public void after(){
        System.out.println("------after---------无论是否异常，都会执行");
    }


    /**
     匹配到类级别
     */
    @Around("within( com.china.school.pojo.User)")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("------around---前------" + joinPoint.getClass());

        if(joinPoint.getArgs().length == 0){

            joinPoint.proceed( );
        }else {
            joinPoint.proceed(  );

        }
        System.out.println("------around-----后----" + joinPoint.getClass());

    }


    @AfterReturning("within(com.china.school.pojo.User)")
    public void returning( ){
        System.out.println("------after-returning---------方法正常结束执行");
    }
    @AfterThrowing("within( com.china.school.pojo.User)")
    public void throwing(){
        System.out.println("------after-throwing---------抛出异常才执行");
    }








}
